App({
  onLaunch: function() {
    // 获取用户信息
    this.getUserInfo();
    
    // 检查登录状态
    this.checkLoginStatus();
    
    // 添加页面路径检查
    this.checkInitialRoute();
  },
getUserInfo: function() {
  try {
    const userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      // 确保用户信息中包含userId字段
      if (!userInfo.userId && userInfo.id) {
        userInfo.userId = userInfo.id;
      }
      if (!userInfo.userId) {
        console.error('用户信息中缺少userId');
        this.logout(); 
        return;
      }
      this.globalData.userInfo = userInfo;
      this.globalData.role = userInfo.role || '';
      console.log('全局用户信息已加载:', this.globalData.userInfo);
    } else {
      console.log('未找到本地存储的用户信息');
    }
  } catch (error) {
    console.error('获取用户信息失败:', error);
  }
  },
  
  checkLoginStatus: function() {
    // 检查登录状态的逻辑
    const token = wx.getStorageSync('token');
    if (token) {
      this.globalData.isLoggedIn = true;
      this.globalData.token = token;
    } else {
      this.globalData.isLoggedIn = false;
    }
    console.log('登录状态:', this.globalData.isLoggedIn ? '已登录' : '未登录');
  },
  
  // 更新用户信息的方法
  updateUserInfo: function(userInfo) {
    if (userInfo) {
      // 确保用户信息中包含userId字段
      if (!userInfo.userId && userInfo.id) {
        userInfo.userId = userInfo.id;
      }
      this.globalData.userInfo = userInfo;
      this.globalData.role = userInfo.role || '';
      wx.setStorageSync('userInfo', userInfo);
      console.log('用户信息已更新:', this.globalData.userInfo);
    }
  },
  
  // 登出方法
  logout: function() {
    // 清除本地存储的用户信息和token
    wx.removeStorageSync('userInfo');
    wx.removeStorageSync('token');
    
    // 重置全局数据
    this.globalData.userInfo = null;
    this.globalData.isLoggedIn = false;
    this.globalData.role = '';
    this.globalData.token = '';
    
    console.log('用户已登出');
    
    // 跳转到登录页
    wx.redirectTo({
      url: '/pages/dengluye/dengluye'
    });
  },
  
  // 检查初始路由
  checkInitialRoute: function() {
    // 获取当前页面路径
    const pages = getCurrentPages();
    const currentPage = pages.length > 0 ? pages[pages.length - 1].route : '';
    
    console.log('当前页面路径:', currentPage);
    
    // 如果尝试访问错误的index页面，重定向到正确的页面
    if (currentPage === 'pages/index/index') {
      const userInfo = this.globalData.userInfo;
      let targetUrl = '/pages/dengluye/dengluye';
      
      // 如果已登录，根据角色跳转到对应首页
      if (userInfo && this.globalData.isLoggedIn) {
        const role = userInfo.role;
        if (role === 'student') {
          targetUrl = '/pages/student/index/index';
        } else if (role === 'teacher') {
          targetUrl = '/pages/teacher/index/index';
        } else if (role === 'counselor') {
          targetUrl = '/pages/counselor/index/index';
        } else if (role === 'admin') {
          targetUrl = '/pages/admin/index/index';
        }
      }
      
      console.log('重定向到正确页面:', targetUrl);
      wx.redirectTo({
        url: targetUrl
      });
    }
  },
  
  globalData: {
    userInfo: null,
    isLoggedIn: false,
    role: '', // 用户角色：student, teacher, counselor, admin
    serverUrl: 'http://localhost:3000', // 后端服务器地址
    token: ''
  }
});